/*	Replication do-file
	Metzger and Jones, "Surviving Phases", PA
	
	24JUN16
	
	
	DESCRIPTION:
		This file provides replication code to generate the paper's 2nd application
		dataset, and to	generate all figures and tables in the paper.  If the relevant 
		table/figure is generated in R, this file notes the R file's name.					
		
	REQUIRED PACKAGES:				
		* four user-written ado files (copy all ado and sthlp files in replication folder to local ado folder)
			// To find loc of local ado folder, type: di "`c(sysdir_personal)'"												
	
	SOFTWARE VERSION:
		We used Stata 14 to generate the following tables and figures.			*/
******************** MUST SET ********************

global dir "C:/Users/Shawna/Desktop/PA replic - DVN/"	// to define the local working directory
															* note: both PC and Mac need forward slash at end of path. 
																														
// ^ 	Run above line.  Each code block is written independently, and can then 
//		be run by highlighting the entire relevant block and executing.


*******************************************************************************	
// APPLICATION 2 - DATASET ASSEMBLY
**************************
cd "$dir"
do "_data assembly master [Svolik 2015].do"


*******************************************************************************	
// TABLES
**************************
// Table 1: Baseline Hazard Comparison - Simulated Data
{
	* Generated in R using msm's simmulti.
	* FILE: "MSM_Sim.R"	 [generates "MonteCarlo_Simulations.RData"]
	* FILE: "Table 1.R"
	
		*** CAUTION: MSM_Sim.R takes ~50 minutes to run on Intel i3 3.4GHz, 4GB RAM
		*		 	 If only interested in replicating table, run "Table 1.R" code only.
}
**************************
// Table 2: Multi-state Model of Democratic Reversals-Restorations
{
	* Generated in R. 
	* FILE: "Tables 2 + 5.R"
	
	
	// Stata equivalent:
	cd "$dir"
	use "3 - svolik 2015 - continuous, no TVC [gap time].dta", clear

	
	// stset
	stset t, f(status) enter(t0)
	drop if(trans==3 | trans==6)

	* MODEL A: UNIQUE COVARIATE EFFECTS
	stcox 	lgdp_1_std1  growth_1_std1  exec_pres1  prevd_mil1  ///
			lgdp_1_std2  growth_1_std2  exec_pres2  prevd_mil2  ///
			lgdp_1_std4  growth_1_std4  exec_pres4  prevd_mil4  ///
			lgdp_1_std5  growth_1_std5  exec_pres5  prevd_mil5  ///
		, breslow nohr strata(trans)

	* MODEL B: COLLAPSED COVARIATE EFFECTS
	qui{
		foreach s in lgdp_1_std growth_1_std exec_pres prevd_mil{
			cap egen `s' = rowtotal(`s'1-`s'6)
		}
		
		cap gen lgdp_1_stdOut = cond(trans==1 | trans==2, lgdp_1_std, 0)	// constrains lgdp_1_std1 & lgdp_1_std2 to be equal
		cap gen lgdp_1_stdIn  = cond(trans==4 | trans==5, lgdp_1_std, 0)	// constrains lgdp_1_std4 & lgdp_1_std5 to be equal
		cap gen prevd_milIn = cond(trans==4 | trans==5, prevd_mil, 0)		// constrains prevd_mil4 & prevd_mil5 to be equal
	}																		// growth_1_std = constrains growth_1_std1, growth_1_std2, growth_1_std4, growth_1_std5 to be equal
																			// exec_pres = constrains exec_pres1, exec_pres2, exec_pres4, exec_pres5 to be equal
	
	#delimit ;
	stcox 	lgdp_1_stdOut  growth_1_std  exec_pres  	prevd_mil1 
														prevd_mil2 
						  			    				  			
			lgdp_1_stdIn   				  				prevd_milIn 				 			  
		
		, breslow nohr strata(trans)
		;
	#delimit cr
	
	* How did we decide which covariates to collapse in Table 2's Model B?
	do "Svolik 2015 - Wald tests [gap time].do"
}
**************************
// Table 3: Fake HoR Data
{
	* Generated by hand.
	
	// To verify:
	cd "$dir"
	use "fake HoR.dta", clear
	
	list subject t0 t from to status if(status==1), noobs
}
**************************
// Table 4: Fake HoR: Hazards and Cumulative Hazards, by Transition
{
	* Generated by hand. 
	
	// To verify:
	cd "$dir"
	use "fake HoR.dta", clear
	
	stset t, enter(t0) fail(status==1)
	qui{
		forvalues v=1/4{
			noi di _dup(80) as gr "-"
			noi di _n(1) as gr "Trans: " as ye `v'
			noi ltable t status if(trans==`v'), h noadjust 	// for hazard, a(t)
			noi sts list if(trans==`v'), cumhaz				// for cumulative hazard, A(t) (which is trivial to compute, given simplicity of the example and the above NP hazards.  But for completeness' sake.)
			noi di _n
		}
	}
}
**************************
// Table 5: Observed Transition Frequencies - Svolik 2015
{
	* Generated in R. 
	* FILE: "Tables 2 + 5.R"
	
	
	// Stata equivalent:
	cd "$dir"
	use "3 - svolik 2015 - continuous, no TVC [gap time].dta", clear
	drop if(trans==3 | trans==6)
	
	// numerator
	tab stage nextStage if(status==1)
	
	// denominator for percentages
	qui{
		cap drop temp
		
		bysort ccodeSwID stage (nextStage): gen temp = _n==1
		
		forvalues s=1/3{
			noi di _n as gr "Stage " `s'
			noi count if(stage==`s' & temp==1)
			local denom = r(N)
			
			count if(status==1 & stage==`s')
			noi di as gr "TOTAL Col %: " as ye r(N)/`denom'*100
		}
		drop temp
	}
}
**************************
// Table 6: Simulation Results: Exiting Transitions
{
	* Generated in R. 
	* FILE: Table 6.R	[requires "MonteCarlo_Simulations.RData"]
}
**************************
// Table 7: Simulation Results: Entering Transitions
{
	* Generated in R. 
	* FILE: Table 7.R	[requires "MonteCarlo_Simulations.RData"]
}
**************************
// Table 8: One mssample Draw
{
	* Generated in R. 
	* FILE: Table 8.R
}
**************************
// Table 9: Simulated Data’s True Parameter Values
{
	* Generated by hand.
}
*

*******************************************************************************	
// FIGURES
**************************
// Figure 1: Illustrative Processes
{
	* Generated by hand.
}
**************************
// Figure 2: Baseline Hazard Comparison
{
	* Generated in R. 
	* FILE: Figure 2.R
}
**************************
// Figure 3: Stage Diagram - Simulated
{
	* Generated by hand.
}
**************************
// Figure 4: Stage Diagram - Maeda
{
	* Generated by hand.
}
**************************
// Figure 5: Transition Probabilities from Democracy
{
	* Generated in R. 
	* FILE: "Svolik - trans probs (sims).R"	 [generates "Simulations.RData"]
	* FILE: "Figure 5.R"
	
		*** CAUTION: Simulation code takes ~50 minutes to run on Intel i3 3.4GHz, 4GB RAM
		*		 	 If only interested in replicating graph, run "Figure 5.R" code only.
	  
**************************
// Figure 6: Transition Probabilities: CR vs. Mstate
{
	// Mstate:
	* Generated in R. 
	* FILE: "Figure 6 - mstate.R" [generates "MS_higdp1.dta"]
	
	
	// CR:
	* Generated in R. 
	* FILE: "Figure 6 - CR.R" [generates "CR_higdp1.dta"]
	
	
	// Once mstate and CR are both run:
	cd "$dir"
	*----------------------------------------------
	// Get all data ready

	foreach p in MS CR{
		use "`p'_higdp1.dta", clear

		rename * *_hi`p'
		rename t_hi`p' t

		tempfile hi`p'
		save `hi`p'', replace
	}

	* last dataset open: hiCR
	*----------------------------------------------
	// Merge everything
	merge t using `hiMS', sort unique

	// Make some graphs.
	cap mkdir "plots"
	cd "plots"

	// High GDP
	{
		* D->D
		#delimit ;
		graph twoway line pstate1_hiCR     t, clpattern(solid) clwidth(medium)  clcolor(black)
				||   line pstate1_l_hiCR   t, clpattern(solid) clwidth(medthin) clcolor(black)
				||   line pstate1_u_hiCR   t, clpattern(solid) clwidth(medthin) clcolor(black)
				||   line pstate1_hiMS     t, clpattern(dash) clwidth(medium)  clcolor(black)
				||   line pstate1_l_hiMS   t, clpattern(dash) clwidth(medthin) clcolor(black)
				||   line pstate1_u_hiMS   t, clpattern(dash) clwidth(medthin) clcolor(black)
				||  ,   

			xscale(lwidth(medthick))
			
			yscale(range(0 1) lwidth(medthick))
			
			ylabel(0(.1)1 , nogrid)
			ymtick(##4, nogrid)
 
			xtitle("Years", size(3))
			ytitle("Pr(D→D)", size(3))
			title("More Developed States, D→D", size(4))
			
			legend(col(1) colfirst pos(5) ring(0) order(1 4) label(1 "CR") label(4 "Mstate") )
			aspect(1) scheme(s2mono) graphregion(color(white)  icolor(none) );
			
			#delimit cr;

			graph export "hi - D-D.png", as(png) width(1056) height(768) replace
			
		* D->Ex
		#delimit ;
		graph twoway line pstate2_hiCR     t, clpattern(solid) clwidth(medium)  clcolor(black)
				||   line pstate2_l_hiCR   t, clpattern(solid) clwidth(medthin) clcolor(black)
				||   line pstate2_u_hiCR   t, clpattern(solid) clwidth(medthin) clcolor(black)
				||   line pstate2_hiMS     t, clpattern(dash) clwidth(medium)  clcolor(black)
				||   line pstate2_l_hiMS   t, clpattern(dash) clwidth(medthin) clcolor(black)
				||   line pstate2_u_hiMS   t, clpattern(dash) clwidth(medthin) clcolor(black)
				||  ,   

			xscale(lwidth(medthick))
			
			yscale(range(0 1) lwidth(medthick))
			
			ylabel(0(.1)1 , nogrid)
			ymtick(##4, nogrid)
					 
			xtitle("Years", size(3))
			ytitle("Pr(D→Ex)", size(3))
			title("More Developed States, D→Ex", size(4))
			
			legend(col(1) colfirst pos(2) ring(0) order(1 4) label(1 "CR") label(4 "Mstate"))
			aspect(1) scheme(s2mono) graphregion(color(white)  icolor(none) );
			
			#delimit cr;

			graph export "hi - D-Ex.png", as(png) width(1056) height(768) replace
			
		* D->En
		#delimit ;
		graph twoway line pstate3_hiCR     t, clpattern(solid) clwidth(medium)  clcolor(black)
				||   line pstate3_l_hiCR   t, clpattern(solid) clwidth(medthin) clcolor(black)
				||   line pstate3_u_hiCR   t, clpattern(solid) clwidth(medthin) clcolor(black)
				||   line pstate3_hiMS     t, clpattern(dash) clwidth(medium)  clcolor(black)
				||   line pstate3_l_hiMS   t, clpattern(dash) clwidth(medthin) clcolor(black)
				||   line pstate3_u_hiMS   t, clpattern(dash) clwidth(medthin) clcolor(black)
				||  ,   

			xscale(lwidth(medthick))
			
			yscale(range(0 1) lwidth(medthick))
			
			ylabel(0(.1)1 , nogrid)
			ymtick(##4, nogrid)
 
			xtitle("Years", size(3))
			ytitle("Pr(D→En)", size(3))
			title("More Developed States, D→En", size(4))
			
			legend(col(1) colfirst pos(2) ring(0) order(1 4) label(1 "CR") label(4 "Mstate"))
			aspect(1) scheme(s2mono) graphregion(color(white)  icolor(none) );
			
			#delimit cr;

			graph export "hi - D-En.png", as(png) width(1056) height(768) replace		
	}
	*
}
**************************
// Figure 7: Stage Diagram - House of Representatives
{
	* Generated by hand.
}
**************************
// Figure 8: Transition Probabilities into Democracy (no CIs)
{
	* Generated in R. 
	* FILE: "Figure 8.R"
}
**************************
// Figure 9: Transition Probabilities into Democracy (with CIs)
{
	* Generated in R. 
	* FILE: "Figure 9.R"
}
